package com.manager.api;

import com.manager.base.BaseResponse;
import com.manager.controller.BaseController;
import com.manager.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author zhou
 * @date 2021/07/10 19:05
 **/
@RestController
@RequestMapping("/api/user")
public class UserController extends BaseController {

  @Resource
  private UserService userService;


  /**
   * 注册接口
   *
   * @param email
   * @param password
   * @param verifyCode
   * @return
   */
  @PostMapping("/register")
  public BaseResponse register(@Param("email") String email, @Param("password")
    String password, @Param("verifyCode") String verifyCode) {
    String remoteAddr = super.getRequest().getRemoteAddr();
    BaseResponse register = userService.register(email, password, remoteAddr, verifyCode);
    if (register.get(BaseResponse.IS_SUCCESS).equals(BaseResponse.IS_SUCCESS)){
      return userService.userLogin(email, password);
    }
    return register;
  }

  /**
   * 登陆接口
   *
   * @param
   * @return
   */
  @PostMapping("/login")
  public BaseResponse register(@Param("account") String account, @Param("password") String password) {
    BaseResponse baseResponse = userService.userLogin(account, password);
    super.getSession().setAttribute("user", baseResponse.get(BaseResponse.DATA_TAG));
    return baseResponse;
  }

  /**
   * 发送邮箱接口
   *
   * @return
   */
  @PostMapping("/mail")
  public BaseResponse mail(String email) {
    return userService.sendMail(email);
  }
}
